package de.ihk.beans;

import java.io.Serializable;
import java.sql.SQLException;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import de.ihk.datamanagment.MysqlConnector;

@ManagedBean
@ViewScoped
public class MenuBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManagedProperty("#{loginBean}")
	private LoginBean login;

	private String password;
	private String password2;

	private MysqlConnector conn = null;

	public MenuBean() {
		conn = new MysqlConnector();
	}

	public void changePw(ActionEvent actionEvent) throws SQLException {
		FacesContext context = FacesContext.getCurrentInstance();
		if (password.equals(password2)) {
			System.out.println(password);
			System.out.println(login.getUsername());
			conn.mysqlUpdateQuery("DELETE FROM admins WHERE id = " + login.getId() + ";");
			conn.addAdminToDb(login.getId(), login.getName(), login.getVorname(), login.getUsername(), password, login.getSuperadmin());
			System.out.println("Password updated!");

			context.addMessage(null, new FacesMessage("Successful", "Password changed!"));
		} else {
			System.out.println("NOT MATCHING");
			context.addMessage(null, new FacesMessage("Error", "Passwords didnt match!"));

		}

	}

	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void setLogin(LoginBean login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

}